package com.fh.controller.system.weixin;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.fh.controller.system.weixin.resp.TextMessage;
import com.fh.controller.system.weixin.wxutil.MessageUtil;

@SuppressWarnings("all")
public class WeixinCoreServer {
	public static String processRequest(HttpServletRequest request) {
		String respMessage = null;
		try {
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			// 发送方账号(open_id)==扫描方
			String fromUserName = requestMap.get("FromUserName");
			// 公众账号
			String toUserName = requestMap.get("ToUserName");
			// 消息类型
			String msgType = requestMap.get("MsgType");
			
			
			// 微信加密签名
			String signature = request.getParameter("signature");
			// 时间戳
			String timestamp = request.getParameter("timestamp");
			// 随机数
			String nonce = request.getParameter("nonce");
			// 随机字符串
			String echostr = request.getParameter("echostr");
			//处理消息
			if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
				//接收文本消息
				TextMessage textMessage = new TextMessage();
				textMessage.setToUserName(fromUserName);
				textMessage.setFromUserName(toUserName);
				textMessage.setCreateTime(new Date().getTime());
				textMessage.setMsgType("text");
				textMessage.setFuncFlag(0);
				textMessage.setContent("您输入的是文本消息");
				respMessage = MessageUtil.textMessageToXml(textMessage);
			}else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)){
				//接收文本消息
				TextMessage textMessage = new TextMessage();
				textMessage.setToUserName(fromUserName);
				textMessage.setFromUserName(toUserName);
				textMessage.setCreateTime(new Date().getTime());
				textMessage.setMsgType("text");
				textMessage.setFuncFlag(0);
				textMessage.setContent("您发送的是图片信息");
				respMessage = MessageUtil.textMessageToXml(textMessage);
			}else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)){
				//接收文本消息
				TextMessage textMessage = new TextMessage();
				textMessage.setToUserName(fromUserName);
				textMessage.setFromUserName(toUserName);
				textMessage.setCreateTime(new Date().getTime());
				textMessage.setMsgType("text");
				textMessage.setFuncFlag(0);
				textMessage.setContent("您发送的是地理位置消息");
				respMessage = MessageUtil.textMessageToXml(textMessage);
				
			}else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)){
				//接收事件推送
				// 事件类型
				String eventType = requestMap.get("Event");
				if(eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)){
					//关注
					TextMessage textMessage = new TextMessage();
					textMessage.setToUserName(fromUserName);
					textMessage.setFromUserName(toUserName);
					textMessage.setCreateTime(new Date().getTime());
					textMessage.setMsgType("text");
					textMessage.setFuncFlag(0);
					textMessage.setContent("感谢关注!");
					respMessage = MessageUtil.textMessageToXml(textMessage);
				}else if(eventType.equals(MessageUtil.EVENT_TYPE_SCAN2)){
					System.out.println("你使用的是微信扫一扫");
					String ScanResult = requestMap.get("ScanResult") + "";
					TextMessage textMessage = new TextMessage();
					textMessage.setToUserName(fromUserName);
					textMessage.setFromUserName(toUserName);
					textMessage.setCreateTime(new Date().getTime());
					textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
					textMessage.setFuncFlag(0);
					textMessage.setContent("你扫一扫的结果是：" + ScanResult);
					respMessage = MessageUtil.textMessageToXml(textMessage);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respMessage;
	}
}
